import junit.framework.Assert;

import org.junit.Test;

public class Medium {

	@Test
	public void testMedium() {
		Medium medium = new Medium();
		int[] array = { 1, 2, 3, 2, 2 };
		Assert.assertEquals(2, medium.findMedium(array));

		int[] array1 = { 1 };
		Assert.assertEquals(1, medium.findMedium(array1));

		int[] array2 = { 1, 2, 1 };
		Assert.assertEquals(1, medium.findMedium(array2));

		int[] array3 = { 1, 1, 2 };
		Assert.assertEquals(1, medium.findMedium(array3));

		int[] array4 = { 2, 1, 1 };
		Assert.assertEquals(1, medium.findMedium(array4));
	}

	int findMedium(int[] array) {
		if (array == null || array.length <= 0) {
			return 0;
		}

		int count = 1;
		int value = array[0];
		for (int i = 1; i < array.length; i++) {
			if (value == array[i]) {
				count++;
			} else {
				count--;
			}

			if (count <= 0) {
				value = array[i];
			}
		}

		return value;
	}
}
